package cc.magicjson.minigame.demo.ddd.adapter.in.web;



import cc.magicjson.minigame.demo.ddd.adapter.in.web.request.CreateGameRequest;
import cc.magicjson.minigame.demo.ddd.adapter.in.web.response.CreateGameResponse;
import cc.magicjson.minigame.demo.ddd.adapter.in.web.response.GameWebDTO;
import cc.magicjson.minigame.demo.ddd.application.GameApplicationService;
import cc.magicjson.minigame.demo.ddd.application.command.CreateGameCommand;
import cc.magicjson.minigame.demo.ddd.application.command.StartGameCommand;
import cc.magicjson.minigame.demo.ddd.application.query.GameQuery;
import cc.magicjson.minigame.demo.ddd.application.query.GameQueryDTO;
import cc.magicjson.minigame.demo.ddd.domain.model.GameId;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/games")
public class GameController {
	private final GameApplicationService gameApplicationService;
	private final GameQuery gameQuery;

	public GameController(GameApplicationService gameApplicationService, GameQuery gameQuery) {
		this.gameApplicationService = gameApplicationService;
		this.gameQuery = gameQuery;
	}

	@PostMapping
	public CreateGameResponse createGame(@RequestBody CreateGameRequest request) {
		GameId gameId = gameApplicationService.createGame(new CreateGameCommand(request.name()));
		return new CreateGameResponse(gameId.value());
	}

	@PostMapping("/{id}/start")
	public void startGame(@PathVariable String id) {
		gameApplicationService.startGame(new StartGameCommand(new GameId(id)));
	}

	@GetMapping("/{id}")
	public GameWebDTO getGame(@PathVariable String id) {
		GameQueryDTO queryDTO = gameQuery.findById(new GameId(id));
		return new GameWebDTO(queryDTO.id(), queryDTO.name(), queryDTO.status().toString());
	}
}